<?php

namespace app\applet\controller\work_area;

use app\common\controller\Applet;
use app\common\model\haikang\ConsumerMsgApply;
use app\common\model\work_area\WorkAreaUser;
use app\common\service\RedisQuantity;
use hg\apidoc\annotation as Apidoc;
use thans\jwt\facade\JWTAuth;

/**
 * @Apidoc\Title("申请相关")
 */
class Apply extends Applet
{

    /**
     * @Apidoc\Title("图片误报申请")
     * @Apidoc\Url("/applet/work_area.Apply/consumerMsgApply")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("hk_consumer_msg_id", type="int", desc="报警图片id")
     * @Apidoc\Param("yj_data_id", type="string", desc="消息回调唯一ID")
     * @Apidoc\Param("msg_name", type="string", desc="报警名称")
     * @Apidoc\Param("timestamp", type="string", desc="报警时间")
     * @Apidoc\Param("device_name", type="string", desc="设备名称")
     * @Apidoc\Param("device_serial", type="string", desc="设备号")
     * @Apidoc\Param("channel_no", type="int", desc="通道号")
     * @Apidoc\Param("image_url", type="string", desc="申请图片")
     * @Apidoc\Param("remarks", type="string", desc="备注")
     * @Apidoc\Returned("data", type="object", desc="对象信息")
     */
    public function consumerMsgApply()
    {
        try {
            $param = $this->request->param();
            $result = JWTAuth::auth();//token验证
            $work_area_user_id = $result['work_area_user_id'];
            Validate([
                'hk_consumer_msg_id|报警图片id' => 'require',
                'msg_name|报警名称' => 'require',
                'timestamp|报警时间' => 'require',
                'device_serial|设备号' => 'require',
                'device_name|设备名称' => 'require',
            ])->check($param);
            if (!RedisQuantity::FrequencyLimit(md5(request()->controller() . request()->action() . $work_area_user_id), 2)) {
                throw new \Exception('请求频繁,稍后再试');
            }
            $consumer_msg_apply = ConsumerMsgApply::where(['hk_consumer_msg_id' => $param['hk_consumer_msg_id']])->cache(600)->find();
            if (!empty($consumer_msg_apply)) throw new \Exception('申请中请等待审核');
            $param['timestamp'] = strtotime($param['timestamp']);
            $param['account'] = WorkAreaUser::where(['id' => $work_area_user_id])->value('account');
            ConsumerMsgApply::create($param);
        } catch (\Exception $e) {
            // 回滚事务

            // 验证失败 输出错误信息
            return ajaxJson(0, $e->getMessage());
        }
        return ajaxJson(1, '成功');
    }


    /**
     * @Apidoc\Title("图片误报详情")
     * @Apidoc\Url("/applet/work_area.Apply/consumerMsgApplyInfo")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("hk_consumer_msg_id", type="int", desc="报警id")
     * @Apidoc\Returned("data", type="object", desc="对象信息",
     *    @Apidoc\Returned("state", type="boolean", desc="状态"),
     * )
     */
    public function consumerMsgApplyInfo()
    {
        try {
            $param = $this->request->param();
            $result = JWTAuth::auth();//token验证
            $work_area_user_id = $result['work_area_user_id'];
            Validate([
                'hk_consumer_msg_id|报警图片id' => 'require'
            ])->check($param);
            if (!RedisQuantity::FrequencyLimit(md5(request()->controller() . request()->action() . $work_area_user_id), 2)) {
                throw new \Exception('请求频繁,稍后再试');
            }
            $consumer_msg_apply = ConsumerMsgApply::where(['hk_consumer_msg_id' => $param['hk_consumer_msg_id']])->cache(600)->find();
            $state = empty($consumer_msg_apply) ? false : true;
        } catch (\Exception $e) {
            // 回滚事务

            // 验证失败 输出错误信息
            return ajaxJson(0, $e->getMessage());
        }
        return ajaxJson(1, '成功', $state);
    }


}